import { Component } from "@angular/core"
import { NavController,App,AlertController} from 'ionic-angular';

import { AppConfig } from '../../../../app/app.config';

import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';
@Component({
    selector: 'page-set',
    templateUrl: 'feedback.html',
})

export class FeedbackPage{
    token:any;
    form={
        content:'',
        type:1
    }
    constructor(
        public navCtrl: NavController,
        public app:App,
        public alertCtrl:AlertController,
        public http:HttpClient,
        private storage:NativeStorage,
        private translate: TranslateService,
    ) {
        this.token = this.storage.getItem('token').then(token=>{
            this.token = token;
        })
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.storage.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    goFeedback(){
        
        if(this.form.content.length<=500){
            this.http.post(AppConfig.getUrl()+'feedback/add?token='+this.token, AppConfig.urlEcode(this.form),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( res => {
                if(res['code']===101){
                    this.alertb('success','Submit successful!');
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
        }else{
            this.alertb('Error!','The number of words cannot exceed 500!');
        }
    }
}